package cn.schoolwow.util.module.query.dictionary.service;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import cn.schoolwow.util.module.query.dictionary.domain.DictionaryContext;
import cn.schoolwow.util.module.query.dictionary.domain.QueryDictionaryOption;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class ReplaceToKeyFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        replaceDataObject(flowContext);
        replaceDataArray(flowContext);
    }

    @Override
    public String name() {
        return "替换数据对象的字典为字典key";
    }

    private void replaceDataObject(FlowContext flowContext){
        QueryDictionaryOption queryDictionaryOption = flowContext.checkInstanceData(QueryDictionaryOption.class);

        if(null==queryDictionaryOption.dataObject){
            return;
        }
        if(queryDictionaryOption.dictionaryContextList.isEmpty()){
            return;
        }
        doReplaceDataObject(queryDictionaryOption.dataObject, flowContext);
    }

    private void replaceDataArray(FlowContext flowContext){
        QueryDictionaryOption queryDictionaryOption = flowContext.checkInstanceData(QueryDictionaryOption.class);

        if(null==queryDictionaryOption.dataArray||queryDictionaryOption.dataArray.isEmpty()){
            return;
        }
        if(queryDictionaryOption.dictionaryContextList.isEmpty()){
            return;
        }
        for(int i=0;i<queryDictionaryOption.dataArray.size();i++){
            JSONObject dataObject = queryDictionaryOption.dataArray.getJSONObject(i);
            doReplaceDataObject(dataObject, flowContext);
        }
    }

    private void doReplaceDataObject(JSONObject dataObject, FlowContext flowContext){
        QueryDictionaryOption queryDictionaryOption = flowContext.checkInstanceData(QueryDictionaryOption.class);

        if(null==dataObject){
            return;
        }
        JSONObject newDataObject = new JSONObject();
        Set<String> removeKeySet = new HashSet<>();
        Set<String> dataKeyNameSet = dataObject.keySet();
        for(String dataKey:dataKeyNameSet){
            DictionaryContext dictionaryContext = QueryInstanceList.newQuery(queryDictionaryOption.dictionaryContextList)
                    .addQuery("fieldName", dataKey)
                    .execute()
                    .getOne();
            if(null==dictionaryContext){
                continue;
            }
            String dataValue = dataObject.getString(dataKey);
            for(String dictionaryKeyString:dictionaryContext.keyValueMap.keySet()){
                String dictionaryValue = dictionaryContext.keyValueMap.getString(dictionaryKeyString);
                boolean match = false;
                if(queryDictionaryOption.ignoreCase){
                    match = dictionaryValue.equalsIgnoreCase(dataValue);
                }else{
                    match = dictionaryValue.equals(dataValue);
                }
                if(match){
                    if(null!=dictionaryContext.replaceFieldName){
                        newDataObject.put(dictionaryContext.replaceFieldName, dictionaryKeyString);
                    }else{
                        newDataObject.put(dictionaryContext.fieldName, dictionaryKeyString);
                    }
                    if(!dictionaryContext.fieldName.equals(dataKey)){
                        removeKeySet.add(dataKey);
                    }
                }
            }
        }
        if(!newDataObject.isEmpty()){
            dataObject.putAll(newDataObject);
        }
        if(!removeKeySet.isEmpty()){
            for(String removeKey:removeKeySet){
                dataObject.remove(removeKey);
            }
        }
    }
}
